package cn.hy.dataSync.config;

import cn.hy.dataSync.utils.MD5Utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@Data
public class JobConfig implements Serializable {
    private static final long serialVersionUID = -4404818842993487435L;

    /**
     * 任务ID生成器
     */
    private static transient final AtomicInteger ID_FACTORY =
            new AtomicInteger(0);

    /**
     * 速率配置
     */
    private SpeedConfig speedConfig;

    /**
     * 错误限制
     */
    private ErrorLimit errorLimit;

    /**
     * 读取器配置
     */
    private ReaderConfig readerConfig;

    /**
     * 写入器配置
     */
    private WriterConfig writerConfig;

    /**
     * 生成任务ID
     */
    int nextTaskId() {
        return ID_FACTORY.incrementAndGet();
    }

    /**
     * 内部使用，系统配置
     */
    private transient CoreConfig coreConfig;

    /**
     * 内部使用
     */
    private transient String jobId;

    @JSONField(serialize = false)
    public String getJobId() {
        if (Objects.isNull(this.jobId)) {
            synchronized (this) {
                if (Objects.isNull(this.jobId)) {
                    this.jobId = MD5Utils.encode(JSON.toJSONString(this));
                }
            }
        }
        return this.jobId;
    }
}
